package edu.zzuli.common;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by Chen-D.W on 2017/12/26.
 */
public class MailUtil implements Runnable{

    String to;
    String code;

    public MailUtil(String to,String code){
        this.to=to;
        this.code=code;
    }

    @Override
    public void run() {
        try {
            /**
             * 创建连接对象，连接邮件服务器
             */
            Properties properties=new Properties();
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.host", "smtp.163.com");
            Session session= Session.getInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("18838971932@163.com","c19960210");
                }
            });
            /**
             * 创建邮件对象
             */
            Message message=new MimeMessage(session);

            /**
             * 发送邮件
             */
            /**
             * 设置发件人
             */
            message.setFrom(new InternetAddress("18838971932@163.com"));
            /**
             * 设置收件人
             */
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
            /**
             * 设置主题
             */
            message.setSubject("来自XXX网站的激活");
            /**
             * 设置邮件正文
             */
            message.setContent("<h1>激活邮件</h1><h3><a href=\"http://localhost:8080/checkServlet?code="+code+"\">http://大科比/?code="+code+"</a></h3>","text/html;charset=UTF-8");

            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
